package org.yelongframework.excel.data.collect.sheet;

import org.yelongframework.excel.data.SheetDatas;
import org.yelongframework.excel.data.fill.sheet.SheetDataFillException;
import org.yelongframework.excel.sheet.ExcelSheet;
import org.yelongframework.util.Assert;

/**
 * 抽象实现
 * 
 * @since 1.0.0
 */
public abstract class AbstractSheetDataCollector<T extends SheetDataCollectScheme> implements SheetDataCollector {

	// ==================================================收集==================================================

	@SuppressWarnings("unchecked")
	@Override
	public final SheetDatas collect(ExcelSheet excelSheet, SheetDataCollectScheme sheetDataCollectScheme)
			throws SheetDataCollectException {
		Assert.notNull(excelSheet, "excelSheet cannot be null");
		Assert.notNull(sheetDataCollectScheme, "sheetDataCollectScheme cannot be null");
		sheetDataCollectScheme.check();
		try {
			return doCollect(excelSheet, (T) sheetDataCollectScheme);
		} catch (ClassCastException e) {
			throw new SheetDataFillException("不匹配的工作表数据收集方案：" + e.getMessage());
		}
	}

	// ==================================================执行收集==================================================

	/**
	 * 执行数据收集
	 * 
	 * @param excelSheet             工作表
	 * @param sheetDataCollectScheme 收集方案
	 * @return 数据集
	 * @throws SheetDataCollectException 数据收集异常
	 */
	protected abstract SheetDatas doCollect(ExcelSheet excelSheet, T sheetDataCollectScheme)
			throws SheetDataCollectException;

}
